# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_user_name);
use MIME::Base64;

sub mm_show_request_access_details($) {
my $id=shift;

	# Validate user has a real login
	$user_name=get_user_name();
	if(!defined($user_name) || $user_name eq "" || $user_name eq "public") {
		return "<tr><td colspan=3>Please login to perform this request.</td></tr>\n";
	}

	my $req_dir=$::GRI_FRONTEND->get_directory("requests");
	my $rdb=new gri2_requests("$req_dir/requests.db");
	my ($rc,$str);
	if(!defined($rdb)) {
		($rc,$str)=gri2_requests::error();
		return "<tr><td colspan=3>" . html_error("Failed to open requests database: $str",1) . "</td></tr>\n";
	}
	# Grab the request information first before worrying about sorting this stuff.
	my $req=$rdb->get_request($id);
	if(!defined($req)) {
		return "<tr><td colspan=3>" . html_error("Request ID $id not found.",1) . "</td></tr>\n";
	}
	my $this=$rdb->decode($req);

	my $s="";

	my $fn1='<font class=stdtextlarge>'; my $fn2='</font>';
	my $fn3='<td class=gri_cell width="90%" style="border: dotted 1px;"><font class=stdtextlarge>'; my $fn4='</font></td>';
	# Ok - show the details of the specified request...
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";
	$s.='<tr><td class=gri_cell>'.$fn1.'ID'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.$this->{id}.$fn4.'</tr>' . "\n";
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";

	$s.='<tr><td class=gri_cell >'.$fn1.'Portal'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.$this->{object}.$fn4.'</tr>' . "\n";
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";
	$s.='<tr><td class=gri_cell >'.$fn1.'Raised'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.$::GRI_FRONTEND->datetime_str($this->{raised}).$fn4.'</tr>' . "\n";
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";
	if(defined($this->{updated}) && $this->{updated} > 0) {
		$s.='<tr><td class=gri_cell >'.$fn1.'Last Updated'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.$::GRI_FRONTEND->datetime_str($this->{updated}).$fn4.'</tr>' . "\n";
	} else {
		$s.='<tr><td class=gri_cell >'.$fn1.'Last Updated'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.'N/A'.$fn4.'</tr>' . "\n";
	}
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";
	$s.='<tr><td class=gri_cell >'.$fn1.'Decision'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3.$this->{decision};
	$::request_in_progress=1;
	if(defined($this->{decision_time}) && $this->{decision_time}>0) {
		$s.=" - " . $::GRI_FRONTEND->datetime_str($this->{decision_time});
		$::request_in_progress=0;
	}
	$s.=$fn4."</tr>\n";
	$s.="<tr><td colspan=3>&nbsp;</td></tr>\n";

	# if we have any voters comments, show them now...

	$s.='<tr><td class=gri_cell valign=top>'.$fn1.'Voters Comments'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3;

	for my $cv (@{$this->{voters}}) {
		$s.="$cv->{who}";
		if(length($cv->{when})) {
			$s.=" - " . $::GRI_FRONTEND->datetime_str($cv->{when});
		}
		if(length($cv->{vote})) {
			if($cv->{vote} ne 'C') {
				$s.=" - voted '$cv->{vote}'";
			}
		}
		$s.="<br>$cv->{text}<p>\n";
	}
	$s.=$fn4."</tr>\n";
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";

	# if we have any owners comments/votes, show them now...

	$s.='<tr><td class=gri_cell valign=top>'.$fn1.'Owners Comments'.$fn2.'</td><td>&nbsp;&nbsp;</td>'.$fn3;

	for my $cv (@{$this->{owners}}) {
		$s.="$cv->{who}";
		if(length($cv->{when})) {
			$s.=" - " . $::GRI_FRONTEND->datetime_str($cv->{when});
		}
		if(length($cv->{vote})) {
			if($cv->{vote} ne 'C') {
				$s.=" - voted '$cv->{vote}'";
			}
		}
		$s.="<br>$cv->{text}<p>\n";
	}
	$s.=$fn4."</tr>\n";
	$s.='<tr><td colspan=3>&nbsp;</td></tr>' . "\n";
	return $s;
}


